package br.unb.ida.midialab.pedparq;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.view.View;

public class TelaInicial extends Activity {
	private SharedPreferences mSettings;
    private PedometerSettings mPedometerSettings;
	//atributos splash
	private Thread mSplashThread;
	private boolean mblnClicou = false;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.telainicial);
		
		mSettings = PreferenceManager.getDefaultSharedPreferences(this);

		//thread para mostrar uma tela de Splash
		mSplashThread = new Thread() {
			@Override
			public void run() {
				try {
					synchronized (this) {
						//espera por 5seg ou sai quando o usuário tocar na tela
						wait(3000);
						mblnClicou = true;
					}
				} catch (InterruptedException e) {
				}
				
				if (mblnClicou){
					//fecha a tela de splash
					finish();
					
					//carrega a atividade pedometro
					Intent iOpcoes = new Intent();
					boolean isConfig = mSettings.getBoolean("isConfig", false);
					if(isConfig){
						iOpcoes.setClass(TelaInicial.this, TelaOpcoes.class);
						startActivity(iOpcoes);
					}else{
						iOpcoes.setClass(TelaInicial.this, TelaInstrucoes.class);
						startActivity(iOpcoes);
					}
				}
			}
		};
		mSplashThread.start();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		//garante que quando o usuário clicar na seta de voltar o sistema
		//deve finalizar a thread
		mSplashThread.interrupt();
	}

	@Override
	public boolean onTouchEvent (MotionEvent event){
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			synchronized (mSplashThread) {
				mblnClicou = true;
				//finaliza o comando wait
				mSplashThread.notifyAll();
			}
		}
		return true;
	}
	/*
	public void pedometro(View view){
		Intent iPedometro = new Intent(this, Pedometer.class);
		startActivity(iPedometro);
	}
	 */
}
